/**
 * CONFIDENTIAL and PROPRIETARY
 *   Copyright (c) ZFen Automotive System Co.,LTD. All rights reserved.
 *
 * FILE INFORMATION
 *   FILE NAME: lv_porting.c
 *   AUTHOR(S): Caven Li
 *
 * BRIEF INFORMATION
 *   LVGL porting process function file.
 */

/*---------------------------------------------------------------------------
 *         Header Files
 *---------------------------------------------------------------------------*/
#include "lv_porting.h"
#include "lvgl.h"
#include "lv_drv_conf.h"
#include <SDL2/SDL.h>
#include "sdl/sdl.h"

/*---------------------------------------------------------------------------
 *         Local Defines
 *---------------------------------------------------------------------------*/
#define MONITOR_HOR_RES  (SDL_HOR_RES)
#define MONITOR_VER_RES  (SDL_VER_RES)
#define MONITOR_BUF_SIZE (MONITOR_HOR_RES * 10)
#define _DEFAULT_SOURCE  /* needed for usleep() */
#define SDL_MAIN_HANDLED /* o fix SDL's "undefined reference to WinMain" issue */

/*---------------------------------------------------------------------------
 *         Local Variables
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
 *         Global Variables
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
 *         Local Functions
 *---------------------------------------------------------------------------*/
/**
 * Abstract:
 *   Tick thread process function.
 *
 * Parameter:
 *   data: Pointer to parameter passing into thread to process.
 *
 * Return:
 *   0: The thread is exited.
 */
static int tick_thread(void *data)
{
    LV_UNUSED(data);

    /* Run thread process. */
    while (1)
    {
        SDL_Delay(5);
        /* Tell LittelvGL that 5 milliseconds were elapsed. */
        lv_tick_inc(5);
    }

    return 0;
}

/*---------------------------------------------------------------------------
 *         Global Functions
 *---------------------------------------------------------------------------*/
/**
 * Abstract:
 *   LVGL porting initialization process function.
 *
 * Parameter:
 *   None.
 *
 * Return:
 *   None.
 */
void lv_porting_init(void)
{
    lv_init();
    monitor_init();
    // SDL创建线程
    SDL_CreateThread(tick_thread, "tick", NULL);

    // 初始化图像缓冲区, 第二个缓冲区(可选)可以传入NULL
    static lv_disp_draw_buf_t disp_buf;
    static lv_color_t buf_1[MONITOR_BUF_SIZE];
    lv_disp_draw_buf_init(&disp_buf, buf_1, NULL, MONITOR_BUF_SIZE);

    /* 注册显示驱动 */
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf     = &disp_buf;
    disp_drv.flush_cb     = monitor_flush;
    disp_drv.hor_res      = MONITOR_HOR_RES;
    disp_drv.ver_res      = MONITOR_VER_RES;
    disp_drv.antialiasing = 1;
    lv_disp_t *disp       = lv_disp_drv_register(&disp_drv);

    /* 注册鼠标驱动 */
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1);
    indev_drv_1.type        = LV_INDEV_TYPE_POINTER;
    indev_drv_1.read_cb     = sdl_mouse_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);
}

/**
 * Abstract:
 *   LVGL porting delay process function.
 *
 * Parameter:
 *   None.
 *
 * Return:
 *   None.
 */
void inline lv_porting_delay(void)
{
    SDL_Delay(5);
}
